{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from decouple import AutoConfig\n",
    "import requests\n",
    "from datetime import datetime\n",
    "import time\n",
    "from twitterutils.session import TwitterAPISession\n",
    "from twitterutils.formatters import get_date\n",
    "import pandas as pd\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = AutoConfig(\".env\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "BEARER_TOKEN = config(\"BEARER_TOKEN\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "tas = TwitterAPISession(BEARER_TOKEN)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define fields to be pulled from Twitter API v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "tweetFields = \",\".join([\"author_id\", \"created_at\", \"lang\", \"public_metrics\", \"conversation_id\"])\n",
    "expansions = \",\".join([\"author_id\", \"referenced_tweets.id\", \"referenced_tweets.id.author_id\", \"entities.mentions.username\", \n",
    "                       \"attachments.poll_ids\", \"attachments.media_keys\", \"in_reply_to_user_id\", \"geo.place_id\"])\n",
    "mediaFields = \",\".join([\"duration_ms\", \"height\", \"media_key\", \"preview_image_url\", \"type\", \"url\", \"width\", \"public_metrics\"])\n",
    "userFields = \",\".join([\"name\", \"username\", \"created_at\", \"description\", \"public_metrics\", \"profile_image_url\"])\n",
    "startDate = get_date(2018, 1, 12)\n",
    "endDate = get_date(2021, 5, 31)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define parameters for each query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "nationalist_params = {\n",
    "    \"query\": '\"papua NKRI\"',\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "pro_government_params = {\n",
    "    \"query\": \"#FaktadiPapua\",\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "change_language_params = {\n",
    "    \"query\": '\"papua teroris\" OR \"papua KKB\"',\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "bin_killing_params = {\n",
    "    \"query\": '\"Papua BIN\" OR \"Papua Nugraha\"',\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": get_date(2021, 4, 24),\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ulmwp_params = {\n",
    "    \"query\": '\"ULMWP\" OR \"wenda\"',\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "special_autonomy_params = {\n",
    "    \"query\": \"otsus\",\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "nduga_killings_params = {\n",
    "    \"query\": \"nduga\",\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "racism_protests_params = {\n",
    "    \"query\": \"rasisme\",\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "plm_params = {\n",
    "    \"query\": '\"#papuanlivesmatter\"',\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": get_date(2020, 5, 1),\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "un_assembly_params = {\n",
    "    \"query\": '\"sidang umum pbb papua\" OR \"unga papua\" OR \"vanuatu papua\" OR \"#sidangpbb\"',\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": startDate,\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "koman_params = {\n",
    "    \"query\": '\"beasiswa veronica koman\"',\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": get_date(2020, 8, 1),\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "zanambani_params = {\n",
    "    \"query\": \"zanambani\",\n",
    "    \"tweet.fields\": tweetFields,\n",
    "    \"expansions\": expansions,\n",
    "    \"media.fields\": mediaFields,\n",
    "    \"user.fields\": userFields,\n",
    "    \"max_results\": 500,\n",
    "    \"start_time\": get_date(2020, 9, 1),\n",
    "    \"end_time\": endDate\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Combine all queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_queries = [\n",
    "    nationalist_params,\n",
    "    pro_government_params,\n",
    "    change_language_params,\n",
    "    bin_killing_params,\n",
    "    ulmwp_params,\n",
    "    special_autonomy_params,\n",
    "    nduga_killings_params,\n",
    "    racism_protests_params,\n",
    "    plm_params,\n",
    "    un_assembly_params,\n",
    "    koman_params,\n",
    "    zanambani_params\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Paginate tweets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_results = dict()\n",
    "for q in combined_queries:\n",
    "        try:\n",
    "            print(q[\"query\"])\n",
    "            paged_results = tas.paginate_tweets(q, 1_000_000, 5)\n",
    "            merged_results = tas.merge_pages(paged_results)\n",
    "            query_results[q.get(\"query\")] = merged_results\n",
    "            time.sleep(60)\n",
    "        except Exception as e:\n",
    "            print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "today = datetime.today().strftime(\"%Y%m%d\")\n",
    "\n",
    "with open(f\"tweets/{today}_combined.json\", \"w\") as f:\n",
    "    json.dump(query_results, f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
